package cn.wolfcode.web.interceptor;

import cn.wolfcode.util.UserContext;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by wolfcode-lanxw
 */
@Component
public class CheckLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //请求是静态资源,handler对应的类 class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler
        //请求是动态资源,handler对应的类class org.springframework.web.method.HandlerMethod
        if(handler instanceof HandlerMethod){
            //拦截到的请求是动态的请求
            //判断是否有登录，放行，没有登录跳转到登录页面
            Object emp = UserContext.getCurrentUser();
            if(emp==null){
                response.sendRedirect("/login.html");
                return false;
            }
        }
        return true;
    }
}
